﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechnicalAnalysisEngine;
using System.Diagnostics;

namespace TechnicalAnalysisEngine.Tests
{
    /// <summary>
    /// Summary description for TestMACD
    /// </summary>
    [TestClass]
    public class TestMACD
    {
        List<double> inputValues;

        int expectedSignalCount;
        int expectedMACDCount;
        int expectedSignalOffset;
        int expectedMACDOffset;

        public TestMACD()
        {
            inputValues = new List<double>()
            {
                3,5,4,6,5,7,5,4,2,4,8,5,4,8,9,5,2,6,8,9,
                3,5,4,6,5,7,5,4,2,4,8,5,4,8,9,5,2,6,8,9,
            };

            expectedMACDOffset = 25;
            expectedSignalOffset = 33;
            expectedMACDCount = 15;
            expectedSignalCount = 7;
        }

        [TestMethod]
        public void TestMACDOffsetsAndNumberOfResults()
        {
            int fastPeriod = 12;
            int slowPeriod = 26;
            int signalPeriod = 9;

            var result = AnalysisEngine.MACD(inputValues, fastPeriod, slowPeriod, signalPeriod);

            Assert.AreEqual(expectedMACDOffset, result.MACDStartIndexOffset, "MACD offset is " + result.MACDStartIndexOffset);
            Assert.AreEqual(expectedSignalOffset, result.SignalStartIndexOffset, "Signal offset is " + result.SignalStartIndexOffset);

            Assert.AreEqual(expectedMACDCount, result.MACD.Count, "MACD point count is " + result.MACD.Count);
            Assert.AreEqual(expectedSignalCount, result.Signal.Count, "Signal point count is " + result.Signal.Count);
        }

        [TestMethod]
        public void TestMACDPerformance()
        {
            int fastPeriod = 12;
            int slowPeriod = 26;
            int signalPeriod = 9;

            Random random = new Random();
            var randomlyGeneratedValues = new List<double>();

            for (int i = 0; i < 10000; i++)
            {
                randomlyGeneratedValues.Add(random.Next(85, 90));
            }

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            var result = AnalysisEngine.MACD(randomlyGeneratedValues, fastPeriod, slowPeriod, signalPeriod, true);

            stopWatch.Stop();
            Console.WriteLine(string.Format("MACD, {0} values, {1} μs", randomlyGeneratedValues.Count, stopWatch.ElapsedTicks / 10));
        }
    }
}
